package util;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;

public class QueryBuilder
{
	private ArrayList<String> attributes;
	private String table;
	private final String defaultId = "id";
	
	public QueryBuilder(String table, String args[])
	{
		this.attributes = new ArrayList<String>();
		for(String arg: args)
		{
			attributes.add(arg);
		}
		this.table = table;
	}
	
	public QueryBuilder()
	{
		// TODO Auto-generated constructor stub
	}

	public String selectQuery(String clause)
	{
		String query = "";
		String aLine = "";
		for(String a:attributes)
		{
			aLine = (aLine.length()==0)?a:String.format("%1$s, %2$s", aLine, a);
		}
		query = String.format("SELECT %1$s FROM %2$s%3$s", aLine,this.table,this.parseClause(clause));
		System.out.println(query);
		return query;
	}
	public String insertQuery()
	{
		//System.out.println("Generating insert query.");
		String query = "";
		String aLine = "";
		String values = "";
		//System.out.println("Generating aLine.");
		for(String a:attributes)
		{
			if(!a.equals(defaultId))
			{
					aLine=aLine.length()==0?a:String.format("%1$s, %2$s", aLine, a);
			}
		}
		int defaultStart = this.valuesHasId()?1:0;
		//System.out.println("Default start: "+defaultStart);
		for(int i=defaultStart; i<attributes.size(); i++)
		{
			values=values.length()==0?"?":String.format("%1$s, ?", values);
		}
		//System.out.println("aLine: "+aLine);
		query = String.format("INSERT into %1$s (%2$s) values (%3$s)", this.table,aLine, values);
		System.out.println(query);
		return query;
	}
	
	public String updateQuery(String clause)
	{
		//System.out.println("Building update query:");
		String query="";
		String aLine = "";
		for(String a:attributes)
		{
			//System.out.println("Building attribute");
			if(!a.equals(defaultId))
			{
				//System.out.println("Attribute is not ID. Building attribute line:");
					aLine=aLine.length()==0?String.format("%1$s=?",  a):String.format("%1$s, %2$s=?", aLine, a);
					//System.out.println(aLine);
			}
		}
		query = String.format("UPDATE %1$s SET %2$s%3$s", this.table,aLine,this.parseClause(clause));
		System.out.println(query);
		return query;
	}
	private boolean valuesHasId()
	{
		boolean valuesHasId = false;
		for(String a:attributes)
		{	
			//System.out.println("value: "+a);
			if(a.equals(defaultId))
			{
				valuesHasId = true;
				//System.out.println("Value is id");
			}
		}
		return valuesHasId;
	}
	public String deleteQuery(String clause)
	{
		String query="";
		query = String.format("DELETE FROM %1$s%2$s", this.table,this.parseClause(clause));
		return query;
	}
	private String parseClause(String clause)
	{
		return (clause.length()==0)?"":String.format(" %1$s", clause);
	}
	private java.sql.Date toSql(java.util.Date utilDate)
	{
		java.util.Calendar cal = Calendar.getInstance();
		java.sql.Date sqlDate = null;
		if(utilDate!=null)
		{
			cal.setTime(utilDate);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);    
			sqlDate = new java.sql.Date(cal.getTime().getTime());
		}
		return sqlDate;
		 //  sql date
		//return sqlDate;
		//System.out.println("utilDate:" + utilDate);
		//System.out.println("sqlDate:" + sqlDate);
	}
	public java.util.Date toUtil(java.sql.Date sqlDate)
	{
		java.util.Date utilDate = null;
		if(sqlDate!=null)
		{
			utilDate = new java.util.Date(sqlDate.getTime());
		}
		return utilDate;
	}
	public PreparedStatement setObject(PreparedStatement ps,int i, Object o)
	{
		try
		{
			if (o.getClass() == String.class) {
				ps.setString(i,(String)o);
		    }
		    if (o.getClass() == Integer.class) {
		    	ps.setInt(i,(Integer)o);
		    }
		    if (o.getClass() == Double.class) {
		    	ps.setDouble(i,(Double)o);
		    }
		    if (o.getClass() == java.util.Date.class) {
		    	//System.out.println("Not formated date: "+o);
		    	if(o.equals(new java.util.Date(0)))
		    	{
		    		ps.setNull(i, java.sql.Types.DATE);
		    	}
		    	else
		    	{
		    		ps.setDate(i, this.toSql((java.util.Date)o));
		    	}
		    	/*
		    	java.util.Date date = (java.util.Date)o;
		    	if (date == null ) 
		    	{ 
	                ps.setNull(i, java.sql.Types.DATE); 
		        } else { 
		        	ps.setDate(i, this.toSql(date)); 
		        } 
		    	*/

		    	//ps.setDate(i,(this.toSql((java.util.Date)o)));
		    	//System.out.println("Formated date:"+this.toSql((java.util.Date)o));
		    }
			
		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ps;
	}
}
